package com.hbean.system.config;

import com.hbean.entity.*;
import com.hbean.service.*;
import com.hbean.utils.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018/10/27 13:16
 * @ModificationHistory: （who whatTime doWhat）
 */

@Component
public class SysCache {

    @Autowired
    private SysDicService sysDicService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysAccService sysAccService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;


    /**
     * description: 根据字典编号获取名称
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public String getSysDicNameByID(String str){

        List<SysDic> sysDics = this.sysDicService.getDataForCache();//从缓存获取
        if(sysDics.size() != 0){
            sysDics  = sysDics.stream().filter(sysDic -> sysDic.getSd02().equals(str)).collect(Collectors.toList());
            if(sysDics.size() != 0){
                return sysDics.get(0).getSd01();
            }
        }
        return null;
    }

    /**
     * description: 根据字典编号获取字典项
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public SysDic getSysDicByID(String str){

        List<SysDic> sysDics = this.sysDicService.getDataForCache();//从缓存获取
        if(sysDics.size() != 0){
            sysDics  = sysDics.stream().filter(sysDic -> sysDic.getSd02().equals(str)).collect(Collectors.toList());
            if(sysDics.size() != 0){
                return sysDics.get(0);
            }
        }
        return null;
    }

    /**
     * description: 根据字典父编号获取字典集合
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public List<SysDic> getSysDicByPiD(String str){
        List<SysDic> sysDics = this.sysDicService.getDataForCache();//从缓存获取
        if(sysDics.size() != 0){
            sysDics  = sysDics.stream().filter(sysDic -> sysDic.getSd03().equals(str)).collect(Collectors.toList());
            if(sysDics.size() != 0){
                return sysDics;
            }
        }
        return null;
    }

    /**
     * description: 根据角色编号获取名称
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public String getSysRoleNameByID(String str){

        List<SysRole> sysRoles = this.sysRoleService.getDataForCache();//从缓存获取
        if(sysRoles.size() != 0){
            sysRoles  = sysRoles.stream().filter(sysDic -> sysDic.getSr02().equals(str)).collect(Collectors.toList());
            if(sysRoles.size() != 0){
                return sysRoles.get(0).getSr01();
            }
        }
        return null;
    }

    /**
     * description: 根据角色父编号获取集合
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public List<SysRole> getSysRoleByPiD(String str){
        List<SysRole> sysRoles = this.sysRoleService.getDataForCache();//从缓存获取
        if(sysRoles.size() != 0){
            sysRoles  = sysRoles.stream().filter(sysRole -> sysRole.getSr03().equals(str)).collect(Collectors.toList());
            if(sysRoles.size() != 0){
                return sysRoles;
            }
        }
        return null;
    }

    /**
     * description: 根据用户ID获取其所具有的角色ID
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public String getSysUserRoleByUserID(String str){
        List<SysUserRole> sysUserRoleRoles = this.sysUserRoleService.getDataForCache();//从缓存获取
        if(sysUserRoleRoles.size() != 0){
            sysUserRoleRoles  = sysUserRoleRoles.stream().filter(sysUserRoleRole -> sysUserRoleRole.getSur01().equals(str)).collect(Collectors.toList());
            if(sysUserRoleRoles.size() != 0){
                StringBuilder stringBuilder = new StringBuilder();
                sysUserRoleRoles.forEach(sysUserRole -> {
                    stringBuilder.append(sysUserRole.getSur02()).append(",");
                });
                return stringBuilder.toString().substring(0,stringBuilder.toString().length()-1);
            }
        }
        return "";
    }

    /**
     * description: 根据菜单编号获取名称
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public String getSysMenuNameByID(String str){

        List<SysMenu> sysMenus = this.sysMenuService.getDataForCache();//从缓存获取
        if(sysMenus.size() != 0){
            sysMenus  = sysMenus.stream().filter(sysDic -> sysDic.getSm02().equals(str)).collect(Collectors.toList());
            if(sysMenus.size() != 0){
                return sysMenus.get(0).getSm01();
            }
        }
        return null;
    }

    /**
     * description: 根据用户编号获取用户
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :
     */
    public SysUser getSysUserByID(String str){

        List<SysUser> sysUsers = this.sysUserService.getDataForCache();//从缓存获取
        if(sysUsers.size() != 0){
            sysUsers  = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(str)).collect(Collectors.toList());
            if(sysUsers.size() != 0){
                return sysUsers.get(0);
            }
        }
        return null;
    }

    /**
     * description: 根据关联业务编号获取附件
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :str 业务编号  flag 附件类型
     */
    public List<SysAcc> getSysAccByYwID(String str,String flag){
        Condition condition = new Condition();
        condition.setParam4(str);
        condition.setParam5(flag);
        return this.sysAccService.getDatas(condition);
    }

    /**
     * description: 根据角色编号获取菜单编号
     * create_user: hjibo
     * create_date: 2018-10-28 15:05
     * param :str 业务编号  flag 附件类型
     */
    public String getMenuIdsByRoleId(String str){

        List<SysRoleMenu> sysRoleMenus =  this.sysRoleMenuService.getDataForCache();//从缓存获取
        if(sysRoleMenus.size() != 0){
            sysRoleMenus = sysRoleMenus.stream().filter(sysRoleMenu -> sysRoleMenu.getSrm01().equals(str)).collect(Collectors.toList());
            if(sysRoleMenus.size() != 0){
                StringBuilder stringBuilder = new StringBuilder();
                sysRoleMenus.forEach(sysRoleMenu -> {
                    stringBuilder.append(sysRoleMenu.getSrm02()).append(",");
                });
                return stringBuilder.substring(0,stringBuilder.length()-1);
            }
        }
        return null;
    }

}
